<?php
/**
 * Created by PhpStorm.
 * User: linhui
 * Date: 2016/12/14
 * Time: 23:26
 */

namespace Admin\Controller;


use Common\Model\SmsLogsModel;

class UserController extends BaseController
{
    public function indexCallback(&$list, $map = array())
    {
        foreach ($list as &$val) {
            $role_info = M('admin_role')->alias('ar')->where('ar.admin_id = ' . $val['user_id'])->join('INNER JOIN __ROLE__ r ON r.role_id = ar.role_id')->field('r.role_name')->find();
            $val['role_name'] = $role_info['role_name'];
        }
    }

    /**
     * 列表页前置条件
     * @return array
     */
    public function _filter(){
        $map = array('is_delete'=>0);
        if($keywords = I('keywords')){
            $map['user_login'] = array('like', "%{$keywords}%");
        }
        return $map;
    }

    /**
     * 新增管理员
     */
    public function add()
    {
        $user_model = D('user');
        $role_model = M('admin_role');
        if (IS_POST) {
            $data = I('post.');
            if (!$data['pass']) {
                $this->responseAjax('用户密码不能为空');
            }
            if ($data['pass'] != $data['repass']) {
                $this->responseAjax('用户密码不一致');
            }

            if (!$data['user_login']) {
                $this->responseAjax('用户名不能为空');
            }

            if (!$data['mobile'] || !preg_match('/^1[3|4|5|7|8][0-9]{9}$/', $data['mobile'])) {
                $this->responseAjax('手机号错误');
            }


            $data['create_time'] = time();
            $data['user_pass'] = sp_password($data['pass']);
            if ($user_model->create($data) && $user_id = $user_model->add()) {
                $role_model->add(array(
                    'admin_id' => $user_id,
                    'role_id' => I('role_id', 0, 'intval'),
                ));
                $this->responseAjax('保存成功', true, array(), U('index'));
            } else {
                $this->responseAjax('保存失败');
            }
        }
        $role_list = M('role')->where(array('status' => 1))->select();
        $this->assign('role_list', $role_list);
        $this->display();
    }


    /**
     * 编辑管理员
     */
    public function edit()
    {
        $user_model = D('user');
        $role_model = M('admin_role');
        $user_info = $user_model->find(I('user_id', 0, 'intval'));
        $role_info = $role_model->where(array('admin_id' => $user_info['user_id']))->find();

        if (IS_POST) {
            $role_id = I('role_id', 0, 'intval');
            if($role_id){
                if ($role_info) {
                    $role_model->where(array('admin_id' => $user_info['user_id']))->save(array('role_id' => $role_id));
                } else {
                    $role_model->add(array(
                        'admin_id' => $user_info['user_id'],
                        'role_id' => $role_id,
                    ));
                }
            }

            if ($user_model->create() && $user_model->save() !== false) {
                if ($user_info['user_id'] == $this->user['user_id']) {
                    $user_info = $user_model->find(I('user_id', 0, 'intval'));
                    $user_info['role_ids'] = M('AdminRole')->where(array('admin_id'=>$user_info['user_id']))->getField('role_id', true);
                    session('user', $user_info);
                }
                $this->responseAjax('保存成功', true, array(), U('index'));
            } else {
                $this->responseAjax('保存失败', false);
            }
        }
        $role_list = M('role')->where(array('status' => 1))->select();
        $this->assign('role_list', $role_list);
        $this->assign('user_info', $user_info);
        $this->assign('role_info', $role_info);
        $this->display();
    }

    /**
     *修改手机号
     */
    public function changeMobile()
    {
        if(IS_POST) {
            $data = I('post.');
            if(!$data['user_id']) {
                $this->responseAjax('参数异常');
            }
            //验证手机
            if(!$data['mobile']) {
                $this->responseAjax('手机号码不能为空');
            }
            $sms = new SmsLogsModel();

            //验证验证码
            if(!($data['code'] && $sms->verify($data['mobile'], $data['code']) != 0)) {
                $this->responseAjax('验证码错误');
            }
            $user_model = M('User');
            if($user_model->where(['user_id'=>$data['user_id']])->save(['mobile'=>$data['mobile']])) {
                $this->responseAjax('修改成功', true);
            } else {
                $this->responseAjax('修改失败');
            }
        }
        $this->display();
    }

    /**
     * 修改密码
     */
    public function modifyPass()
    {
        $model = D('User');
        $user_id = $this->user['user_id'];
        $user_info = $model->find($user_id);
        if (IS_POST) {
            $data = I('post.');
            if (!$data['newpass'] || $data['newpass'] != $data['renewpass']) {
                $this->responseAjax('新密码输入错误', false);
            }
            if (sp_password($data['mpass']) != $user_info['user_pass']) {
                $this->responseAjax('旧密码不正确', false);
            }

            if ($model->where(array('user_id' => $user_id))->save(array('user_pass' => sp_password($data['newpass'])))) {
                $this->responseAjax('修改密码成功', true, array(), U('Index/index'));
            }
            $this->responseAjax('修改密码失败', false);
        }

        $this->assign('user_info', $user_info);
        $this->display();
    }

    /**
     * 个人信息设置
     */
    public function setting()
    {
        $model = D('User');
        $user_id = $this->user['user_id'];
        $info = $model->find($user_id);
        $this->assign('info', $info);
        $this->display();
    }
}